package com;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Transaction;

import struct.CommentInfo;
import struct.ItemInfo;
import struct.SellerInfo;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;

import database.Helper;
@SuppressWarnings("serial")
public class RateServlet extends HttpServlet 
{//client-server style (get input from client, then redirect output)
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException 
	{
		String msg = "";
		String seller = req.getParameter("seller");
		String username = req.getParameter("username");
		String rating = req.getParameter("rating");
		boolean dejaRated = false;
		
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		
		if(rating == ""){
			//empty
			msg = "Error. Please enter a rating.";
			req.setAttribute("msg", msg);

			
			System.out.println(msg);

			req.getRequestDispatcher("error.jsp").forward(req, resp);
		}
		else if(!isInt(rating)){
			//non integer
			msg = "Error. Please enter an integer for rating.";
			req.setAttribute("msg", msg);

			
			System.out.println(msg);

			req.getRequestDispatcher("error.jsp").forward(req, resp);
		}else{
			if(Integer.parseInt(rating)>10 || Integer.parseInt(rating)<0){
				
				//out of range
				msg = "Error. Please enter an integer in the range of 0 to 10.";
				req.setAttribute("msg", msg);

				
				System.out.println(msg);

				req.getRequestDispatcher("error.jsp").forward(req, resp);
				
			}else{
				
				Query qRatings = new Query("Rating").addFilter("SELLER", FilterOperator.EQUAL,seller);
				Iterable<Entity> ratings = ds.prepare(qRatings).asIterable();
				for(Entity r : ratings ){
					if(((String)r.getProperty("BUYER")).equals(username)){
						dejaRated = true; //user has already rated
					}
				}
				
				if(dejaRated){
					//alreaedy rated
					msg = "Error. You have already rated this seller.";
					req.setAttribute("msg", msg);

					
					System.out.println(msg);

					req.getRequestDispatcher("error.jsp").forward(req, resp);
				}else{
					for (int j = 0; j < 10; j++) {
						Transaction tx = ds.beginTransaction();
						try {
							Entity e = new Entity("Rating");
							e.setProperty("RATE", rating);
							e.setProperty("BUYER", username);
							e.setProperty("SELLER",seller);
							//Blackboard style
							ds.put(tx, e);
							tx.commit();
							break;
							
							
						} catch (ConcurrentModificationException ed) {
							// continue
						} finally {
							if (tx.isActive())
								tx.rollback();
						}
					}					
					req.setAttribute("username", username);
					req.setAttribute("sellerInfo", Helper.GetSellerInfo(seller));
					req.setAttribute("commentInfo", Helper.GetCommentInfo(seller));
			
					req.getRequestDispatcher("sellerInfo.jsp").forward(req, resp);
				}				
			}			
		}
	}
	
	public static boolean isInt (String str){
		try{
			int i = Integer.parseInt(str);
		}catch(NumberFormatException e){
			return false;
		}
		return true;
	}
	
}
